package com.apes.scm.masterdata.enumeration.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：枚举类型
 *
 * @author xul
 * @create 2018-03-05 11:12
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = EnumerationType.CONST_TABLE_NAME)
public class EnumerationType extends Model {

    public static final String CONST_TABLE_NAME = "mas_enumeration_type";

    @Id
    @Column(name = "id", length = 60)
    @Comment("编码")
    private String id;

    @Column(name = "description", length = 120, nullable = false)
    @Comment("描述")
    private String description;  //描述

    @Column(name = "sys_inner", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("系统内定标记")
    private boolean sysInner = false; //系统内定标记

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效标记")
    private boolean valid = true; //有效

    @OneToMany(mappedBy = "type", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("code ASC")
    private Set<Enumeration> enumerations = new HashSet(); //枚举值

    public Set<Enumeration> getEnumerations() {
        for (Enumeration item : enumerations) {
            item.setType(this);
        }
        return enumerations;
    }

}
